package com.sntk.organauthomanager.domain.model;

import com.sntk.organauthomanager.infrastructure.exception.DataNoLegalException;
import com.sntk.organauthomanager.infrastructure.exception.ObjectDisbaledException;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * @ClassName: User
 * @author: wgchao
 * @createTime: 2020/7/17 3:21 PM
 * @Description: 用户
 */
@Getter
public class User extends AggregateRoot {

    //账户名-唯一
    private String uname;

    //机构编号
    private String institutionNumber;

    //认证密码(加密后)
    private String password;

    //用户名称
    private String userName;

    //用户对应his编码
    private String hisCode;

    //用户默认his科室编码
    private Long hisDepartmentCode;

    //速记码
    private String shorthandCode;

    //联系电话
    private String phoneNumber;

    //用户类型 管理员/普通用户
    private UserType userType;

    //备注
    private String remark;

    //禁用标识
    private boolean disabled;

    public User(Long id, String uname, String institutionNumber, String password, String userName,
                String hisCode, Long hisDepartmentCode, String shorthandCode,
                String phoneNumber, String remark, UserType userType, boolean disabled, Long version) {
        if(StringUtils.isEmpty(uname)){
            throw new DataNoLegalException("账户名不能为空");
        }
        if(StringUtils.isEmpty(institutionNumber)){
            throw new DataNoLegalException("所属机构编号不能为空");
        }
        if(StringUtils.isEmpty(password)){
            throw new DataNoLegalException("认证密码不能为空");
        }
        if(StringUtils.isEmpty(userName)){
            throw new DataNoLegalException("用户名称不能为空");
        }
        if(StringUtils.isEmpty(shorthandCode)){
            throw new DataNoLegalException("速记码不能为空");
        }
        if(userType == null){
            throw new DataNoLegalException("用户类型不能为空");
        }
        setId(id);
        this.uname = uname;
        this.institutionNumber = institutionNumber;
        this.password = password;
        this.userName = userName;
        this.hisCode = hisCode;
        this.hisDepartmentCode = hisDepartmentCode;
        this.shorthandCode = shorthandCode;
        this.phoneNumber = phoneNumber;
        this.remark = remark;
        this.disabled = disabled;
        this.userType = userType;
        setVersion(version);
    }

    public static User generate(String uname, Organization organization, String password, String userName,
                                String hisCode, Long hisDepartmentCode, String shorthandCode,
                                String phoneNumber, String remark, int userType){
        organization.normalException();
        return new User(-1L, uname, organization.getInstitutionNumber(), password, userName, hisCode, hisDepartmentCode, shorthandCode,
                phoneNumber, remark, UserType.get(userType), false, null);
    }

    public void modifyUserInfo(String userName, String hisCode, Long hisDepartmentCode,
                               String shorthandCode, String phoneNumber, String remark, boolean disabled, Integer userType){

        if(StringUtils.isEmpty(userName)){
            throw new DataNoLegalException("用户名称不能为空");
        }
        this.userName = userName;
        this.hisCode = hisCode;
        this.hisDepartmentCode = hisDepartmentCode;
        this.shorthandCode = shorthandCode;
        this.phoneNumber = phoneNumber;
        this.remark = remark;
        this.disabled = disabled;
        if(userType == null){
            throw new DataNoLegalException("用户类型不能为空");
        }
        UserType type = UserType.get(userType);
        if(type == null){
            throw new DataNoLegalException("用户类型不存在");
        }
        this.userType = type;
    }

    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!normal()){
            throw new ObjectDisbaledException(this.userName+"用户不可用");
        }
    }

    //修改密码
    public void modifyPassword(String newPwd){
        this.password = newPwd;
    }
}
